- [Syntax](#org31d4f71)
- [Logs](#org720f95f)
  - [General log files](#orgaa6ddaf)
  - [Logging each job](#org6106ba6)



<a id="org31d4f71"></a>

# Syntax

The schedule specification syntax is:

```crontab
minute hour day month weekday
```


<a id="org720f95f"></a>

# Logs


<a id="orgaa6ddaf"></a>

## General log files

General logs of crontab can be viewed using the following command:

```shell
grep CRON /var/log/syslog
```

This probably requires to have `rsyslog` installed.

If no logs show up, check the file `/etc/rsyslog.d/50-default.conf` for a commented out line like `#cron.*` and uncomment it. Then restart `rsyslog` using `service rsyslog restart`.


<a id="org6106ba6"></a>

## Logging each job


### Variant 1 - redirect stderr and output to log file

```crontab
0 13 * * * call_to_script >> /home/log/myscript.log 2>&1
```


### Variant 2 - redirect stderr and stdout to separate files

```crontab
42 17 * * * command >>stdout.log 2>>stderr.log
```


### Variant 3 - redirect and display using tee

```crontab
0 13 * * * call_to_script 2>&1 | tee --append log_file 2>&1
```

This has the advantage, that the cronjob is observable, when run manually.


### Variant 4 - redirect and display without depending on tee in docker containers

```crontab
0 9 * * * command > /proc/1/fd/1 2>&1
```

The `/proc/1/fd/1` is explained as follows: `/proc/PID/fd/1` where `/proc` is a conventional location, then follows the PID of the process whose file descriptors will be used, then `/fd`, which stands for "file descriptors" and then `/1` for stdout or `/2` for stderr.

Docker always uses the stdout from process with PID 1 as the docker log stream, so redirecting output to `/proc/1/fd/1` will make the output visible in `docker logs ...` output.


### Variant 5 - redirect and display without depending on tee

```crontab
0 9 * * * command > /proc/$(cat /var/run/crond.pid)/fd/1 2>&1
```

The `/proc/1/fd/1` is explained as follows: `/proc/PID/fd/1` where `/proc` is a conventional location, then follows the PID of the process whose file descriptors will be used, then `/fd`, which stands for "file descriptors" and then `/1` for stdout or `/2` for stderr.

`$(cat /var/run/crond.pid)` will find the PID of the running cron process.
